package com.hujiang.basic.search.common.converter.req;

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.hujiang.basic.search.common.component.HJElasticSearchClient;
import com.hujiang.basic.search.model.HJBulkDeleteRequest;

import java.util.Map;

@Component
public class BulkDeleteRequestConverter extends BulkRequestConverter<HJBulkDeleteRequest> {

    @Autowired
    private HJElasticSearchClient client;

    @Override
    public BulkRequestBuilder convert(HJBulkDeleteRequest in) {
        BulkRequestBuilder bulk = client.prepareBulk();

        for (int i = 0; i < in.getSources().size(); i++) {
            JSONObject json = in.getSources().getJSONObject(i);
            String id = extractId(json);
            DeleteRequestBuilder drb = client.preparedelete(in.getIndex(), in.getType(), id);
            bulk.add(drb);
        }
        return bulk;
    }
}
